package com.article.controller;

import com.article.model.ArticleInfo;
import com.article.service.ArticleInfoService;
import com.article.utils.PageUtil;
import com.article.utils.StringUtil;
import com.github.pagehelper.PageInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/4.
 */
@RestController
@RequestMapping("/")
public class IndexController {
    private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private ArticleInfoService articleInfoService;

    @RequestMapping("/index.html")
    public ModelAndView index(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                              @RequestParam(value = "typeId", required = false) String typeId,
                              @RequestParam(value = "releaseDateStr", required = false) String releaseDateStr,
                              HttpServletRequest request) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("typeId", typeId);
        map.put("releaseDateStr", releaseDateStr);

        List<ArticleInfo> articleInfoList = new ArrayList<ArticleInfo>();
//        PageHelper.startPage(pageIndex, 10);
        PageInfo<ArticleInfo> p = articleInfoService.listByPage(pageIndex, 10, map);
        articleInfoList = p.getList();
        //jsoup获取图片
        for (ArticleInfo articleInfo : articleInfoList) {
            List<String> imagesList = articleInfo.getImagesList();
            String content = articleInfo.getContent();
            Document doc = Jsoup.parse(content);
            Elements jpgs = doc.select("img[src~=(?i)\\.(png|jpe?g)]"); //　查找扩展名img[src$=.jpg]是jpg的图片
            for (int i = 0; i < jpgs.size(); i++) {
                Element jpg = jpgs.get(i);
                imagesList.add(jpg.toString());
                if (i == 2) {
                    break;
                }
            }
        }
//        PageInfo<ArticleInfo> p = new PageInfo<ArticleInfo>(articleInfoList);
        mav.addObject("articleList", p.getList());
        // 查询参数
        StringBuffer param = new StringBuffer();
        if (StringUtil.isNotEmpty(typeId)) {
            param.append("typeId=" + typeId + "&");
        }
        if (StringUtil.isNotEmpty(releaseDateStr)) {
            param.append("releaseDateStr=" + releaseDateStr + "&");
        }
        mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath() + "/index.html", p.getTotal(), pageIndex, 10, param.toString()));
        mav.addObject("mainPage", "foreground/article/list.ftl");
        mav.addObject("pageTitle", "万恒文档");
        mav.setViewName("mainTemp");
        return mav;
    }
}
